package exercise04;

import java.util.Random;

public class MyRedPack extends Thread{
    static int count = 3;
    static double money = 100;
    static final double MIN = 0.01;

    private double randomDouble(double minVal,double maxVal){
        return minVal+Math.random()*(maxVal-minVal);
    }

    @Override
    public void run() {
        synchronized (MyRedPack.class){
            if (count == 0){
                System.out.println(getName()+"没有抢到红包");
            }else{
                double prize = 0;
                if (count == 1){
                    prize = money;
                }else{
                    double bounds = money - (count-1)*MIN;
                    // Random r = new Random();
                    // prize = r.nextDouble(bounds);//jdk17才支持
                    prize = randomDouble(MIN,bounds); //jdk17才支持
                    if (prize < MIN){
                        prize = MIN;
                    }
                }
                money = money - prize;
                count--;
                System.out.println(getName()+"抢到"+prize+"元");
            }
        }
    }
}
